package org.tigris.atlas.jmx.weblogic;

import java.util.Properties;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.TargetMBean;

/**
 * Manages the configuration of a JMS Queue
 */
public class JMSQueueConfiguration extends AbstractResourceConfiguation {

	static final String JMS_QUEUE_NAME = "jms.queue.name";
	static final String ATTRIBUTE_REDELIVERY_LIMIT = "RedeliveryLimit";
	static final String JMS_QUEUE_REDELIVERY_LIMIT = "jms.queue.redelivery.limit";
	static final String JMS_QUEUE_JNDI_NAME = "jms.queue.jndi.name";
	private final String DEFAULT_REDELIVERY_LIMIT = "10";	
	static final String JMS_QUEUE = "JMSQueue";
	
	private Log log = LogFactory.getLog(JMSQueueConfiguration.class);	
	
	/**
	 * Creates a new instance of this configuration object
	 * @param mBeanName The name of the resource to manage
	 * @param properties The properties from which to manage the resorce
	 */
	public JMSQueueConfiguration(String mBeanName, Properties properties) {
		super(mBeanName, properties);
	}
	
	/**
	 * @see org.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#getLog()
	 */
	protected Log getLog() {
		return log;
	}

	/**
	 * @see org.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#getMBeanType()
	 */
	protected String getMBeanType() {
		return JMS_QUEUE;
	}

	/**
	 * @see org.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#setMBeanAttributes(weblogic.management.configuration.TargetMBean[], weblogic.management.WebLogicMBean, weblogic.management.MBeanHome)
	 */
	protected void setMBeanAttributes(TargetMBean[] targetServers, WebLogicMBean mBean, MBeanHome home) {
		String jndiName = properties.getProperty(JMS_QUEUE_JNDI_NAME);
		String redeliveryLimit = properties.getProperty(JMS_QUEUE_REDELIVERY_LIMIT);
		
		if ((redeliveryLimit == null) || (!NumberUtils.isNumber(redeliveryLimit))) {
			log.equals("Redelivery limit '" + redeliveryLimit + "' is not valid, defaulting to '" + DEFAULT_REDELIVERY_LIMIT + "'!");
			redeliveryLimit = DEFAULT_REDELIVERY_LIMIT;
		}
		
		addAttribute(ATTRIBUTE_JNDI_NAME, jndiName, mBean);
		addAttribute(ATTRIBUTE_REDELIVERY_LIMIT, new Integer(redeliveryLimit), mBean);

	}

}
